/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.toko.dao.impl;

import com.toko.dao.CustomerDAO;
import com.toko.model.Customer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Benny
 */
public class CustomerDAOImpl implements CustomerDAO {

    private Connection conn;

    public CustomerDAOImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public void insert(Customer customer) throws Exception {
        int result = 0;
        String sql = "INSERT INTO customer(name,address,phone,email,username,password) VALUES(?,?,?,?,?,?)";
        try {
            PreparedStatement ps = this.conn.prepareStatement(sql);
            ps.setString(1, customer.getName());
            ps.setString(2, customer.getAddress());
            ps.setString(3, customer.getPhone());
            ps.setString(4, customer.getEmail());
            ps.setString(5, customer.getUsername());
            ps.setString(6, customer.getPassword());
            result = ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception("category gagal disimpan" + e.getMessage());
        }
    }

    @Override
    public void delete(int id) throws Exception {
        String sql = "DELETE FROM customers WHERE id=?";
        PreparedStatement st = this.conn.prepareStatement(sql);
        st.setLong(1, id);
        st.executeUpdate();
        st.close();
    }

    @Override
    public void update(int oldId, Customer customer) throws Exception {
        String sql = "UPDATE customer SET name=?,address=?,phone=?,email=?,username=?,password=? WHERE id=?";
        PreparedStatement ps = this.conn.prepareStatement(sql);
        ps.setString(1, customer.getName());
        ps.setString(2, customer.getAddress());
        ps.setString(3, customer.getPhone());
        ps.setString(4, customer.getEmail());
        ps.setString(5, customer.getUsername());
        ps.setString(6, customer.getPassword());
        ps.executeUpdate();
        ps.close();
    }

    @Override
    public Customer getCustomer(int id) throws Exception {
        Customer customer = null;
        String sql = "SELECT name,address,phone,email,username,password FROM customer "
                + "WHERE id=?";
        PreparedStatement st = this.conn.prepareStatement(sql);
        st.setLong(1, id);
        ResultSet rs = st.executeQuery();
        if (rs.next()) {
            customer = new Customer();
            customer.setName(rs.getString(2));
            customer.setAddress(rs.getString(3));
            customer.setPhone(rs.getString(4));
            customer.setEmail(rs.getString(5));
            customer.setUsername(rs.getString(6));
            customer.setPassword(rs.getString(7));
        }
        return customer;

    }

    @Override
    public List<Customer> getCustomers() throws Exception {
        List<Customer> customers = new ArrayList<Customer>();
        String sql = "SELECT name,address,phone,email,username,password FROM customer";
        PreparedStatement st = this.conn.prepareStatement(sql);
        ResultSet rs = st.executeQuery();
        while (rs.next()) {
            Customer customer = new Customer();
            customer.setId(rs.getInt(1));
            customer.setName(rs.getString(2));
            customer.setAddress(rs.getString(3));
            customer.setPhone(rs.getString(4));
            customer.setEmail(rs.getString(5));
            customer.setUsername(rs.getString(6));
            customer.setPassword(rs.getString(7));
            customers.add(customer);
        }
        return customers;
    }

    @Override
    public Customer login(String username) throws Exception {
       Customer customer = null;
        String sql = "SELECT name,address,phone,email,username,password FROM customer" +
                "WHERE username=?";
        PreparedStatement st = this.conn.prepareStatement(sql);
        st.setString(1, username);
       
        ResultSet rs = st.executeQuery();
        if(rs.next()){
            customer = new Customer();
            customer.setId(rs.getInt(1));
            customer.setName(rs.getString(2));
            customer.setAddress(rs.getString(3));
            customer.setPhone(rs.getString(4));
            customer.setEmail(rs.getString(5));
            customer.setUsername(rs.getString(6));
            customer.setPassword(rs.getString(7));
        }
        return customer;
    }
}
